package com.example.goodrubish.Garbage;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.goodrubish.R;

import java.util.ArrayList;
import java.util.List;

public class QuizActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private QuestionAdapter questionAdapter;
    private DatabaseHelper dbHelper;
    private List<Question> questionList;
    private TextView questionNumber;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);

        dbHelper = new DatabaseHelper(this);
        questionList = new ArrayList<>();
        questionNumber = findViewById(R.id.question_number);
        // 使用 try-with-resources 自动关闭数据库
        SQLiteDatabase database = dbHelper.openDatabase();
        try {
            Cursor cursor = database.rawQuery("SELECT * FROM question ORDER BY RANDOM() LIMIT 5", null);
            while (cursor.moveToNext()) {
                Question question = new Question();
                question.ID = cursor.getInt(0);
                question.question = cursor.getString(1);
                question.answerA = cursor.getString(2);
                question.answerB = cursor.getString(3);
                question.answerC = cursor.getString(4);
                question.answerD = cursor.getString(5);
                question.answer = cursor.getInt(6);
                question.explaination = cursor.getString(7);
                questionList.add(question);
            }
            cursor.close();
        } catch (SQLiteException e) {
            e.printStackTrace();
            showErrorMessage("Failed to load questions");
        }
        viewPager = findViewById(R.id.view_pager);
        questionAdapter = new QuestionAdapter(this.getSupportFragmentManager(), questionList);
        viewPager.setAdapter(questionAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {

                 //更新题号和按钮
                questionNumber.setText(String.format("%d/%d", position + 1, 5));
                Fragment fragment = questionAdapter.getItem(position);
                if (fragment instanceof QuestionFragment) {
                    QuestionFragment questionFragment = (QuestionFragment) fragment;
                  //  questionFragment.setupButtons(viewPager,position);


                }

            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }

            @Override
            public void onPageScrollStateChanged(int state) { }
        });
    }

    private void showErrorMessage(String message) {
        // 显示错误消息给用户
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }

    private void updateCurrentQuestionPosition(int position) {
        // 更新当前题目数的逻辑
    }
}